<?php
include("../../tools/session.php");
include("../../tools/controls.php");

// 如果登录失败、无权限访问，那么采取相应措施
if (!($is_login_success === true)) { session_hop_page("../index.php"); exit(); }
if (!power_check(0)) { include("tpl-inc-wp.php"); $tpl->display($oa_tpl_path . "/no-power.html"); exit(); }

/*-- 以下是实际内容 --*/
$page_status = $_POST["page_status"];
$per_page = 10;

include("../../tools/mysql.php");
db_connect();

if (!$page_status)
{
	include("tpl-inc-wp.php");
    
	$sql = "SELECT ceil(count(id)/" . $per_page . ") num FROM user";
	$re = db_query_once($sql);
		
	$tpl->assign("page_num", $re["num"]);

	$tpl->display($oa_tpl_path . "/default/section_allocate.html");
}
elseif ($page_status == 1)
{
    	header("content-type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	echo "<people>\n";

	$page = $_POST["page"];
	$page = (!$page) ? 1 : $page;

	$page_min = $per_page * ($page - 1); 
	$sql = "SELECT id, username, section_id, register_time FROM user WHERE deleted=0 ORDER BY register_time DESC LIMIT " . $page_min . "," . $per_page;
	$sql_whole = "SELECT count(id) num FROM user WHERE deleted=0";
    $sql_section = "SELECT * FROM section";
      
	$re = db_query($sql);
	$num = count($re);
	$whole = db_query_once($sql_whole);
    
	$wholepage = ceil($whole["num"] / $per_page);
    
    $options_section = array(0 => "未分配部门");
    $re_section = db_query($sql_section);
    foreach ($re_section as $v) { $options_section[$v["section_id"]] = $v["zh_name"]; }

	echo "<perpage>" . $per_page . "</perpage>\n";
	echo "<wholepage>" . $wholepage . "</wholepage>\n";
	
	$str = "";
	foreach ($re as $v)
	{
	    $options_str = "";
	    $section_id = $v["section_id"];
	    $str .= "<item>\n";
	    $str .= "<id>" . $v["id"] . "</id>\n";
	    $str .= "<username>" . $v["username"] . "</username>\n";
	    $str .= "<register_time>" . $v["register_time"] . "</register_time>\n";
	    $str .= "<options><![CDATA[\n";
	    foreach ($options_section as $key => $value)
	    {
		$str .= "<option value=\"" . $key . "\" " . ($key == $section_id ? " selected=\"selected\"" : "") . ">" . $value . "</option>";
	    }
	    $str .= "]]></options>\n";
	    $str .= "</item>\n";
	}
	
	echo $str;
	
	echo "</people>\n";
}
elseif ($page_status == 2)
{
    $id = $_POST["id"];
    $section_id = $_POST["section_id"];
    $sql = "UPDATE user SET section_id='" . $section_id . "' WHERE id='" . $id . "'";
    echo (db_exec($sql)) ? "success!" : "failed!";
}
?>

